package cn.zyx.controller;

import cn.zyx.dao.OilDao;
import cn.zyx.entity.Oil;
import cn.zyx.service.OilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Oil)表控制层
 *
 * @author makejava
 * @since 2022-01-03 15:43:20
 */
@RestController
@RequestMapping("oil")
public class OilController {
    /**
     * 服务对象
     */
    @Resource
    private OilService oilService;

    @Resource
    private OilDao oilDao;
    @Autowired
    private ReturnPayApi returnPayApi;

    /**
     * 根据openid 查询
     */
    @RequestMapping("selopenid")
    public List<Oil> selopenid(String openid){
        return oilService.queryopenId(openid);
    }
    /**
     * 根据订单编号查询
     */
    @RequestMapping("selnumber")
    public List<Oil> selnumber(String number){
        return oilService.querynumber(number);
    }

    /**
     * 油卡充值
     */
    @RequestMapping("add")
    public Oil add(@RequestBody Oil oil){
        OilController oilController=new OilController();
        String time = oilController.time();
        oil.setDstatus("1");
        oil.setStartDate(time);
        oilService.insert(oil);
        return oil;
    }

    /**
     * 通过地区名&充值金额下拉框&充值状态
     *
     * @param
     * @return
     */
    @RequestMapping("sel")
    public Map<String, Object> query(@RequestParam(defaultValue = "1") Integer page, Integer limit,String dphone,String oilname, String dmoney,String dstatus,String startDate,String endDate) {
        List<Oil> oils = this.oilService.queryAllByLimit((page - 1) * limit, limit, dphone, oilname,  dmoney, dstatus, startDate, endDate);
        int count = oilDao.count(dphone, oilname, dmoney, dstatus, startDate, endDate);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("data", oils);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }
    /**
     * 将未充值用户进行充值
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("cupd")
    public Map<String, Object> cupd(Integer id) {
        Oil oil = new Oil();
        oil.setId(id);
        oil.setDstatus("2");
        OilController oilController = new OilController();
        String time = oilController.time();
        oil.setEndDate(time);
        oilService.update(oil);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将未充值用户进行退款
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("tupd")
    public Map<String, Object> tupd(Integer id, String orderNum, String money, String desc, HttpServletResponse response) {
        Oil oil1 = oilService.queryById(id);
//        int a = (int) (Float.valueOf(oil1.getFmoney())*100);
//        returnPayApi.refund(oil1.getNumber(),"",String.valueOf(a), response);
//        Map<String, Object> refund = returnPayApi.refund(oil1.getNumber(), "", "1", response);
        Oil oil = new Oil();
        oil.setId(id);
        oil.setDstatus("3");
        OilController oilController = new OilController();
        String time = oilController.time();
        oil.setEndDate(time);
        oil.setDesc(desc);
        oil.setFmoney(money);
        oil.setNumber(orderNum);
        oilService.update(oil);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将充值完成或者退款成功记录进行删除
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("dupd")
    public Map<String, Object> dupd(Integer id) {
        Oil oil = new Oil();
        oil.setId(id);
        oil.setIsdel(1);
        OilController oilController = new OilController();
        String time = oilController.time();
        oil.setEndDate(time);
        oilService.update(oil);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("dupds")
    public Map<String, Object> dupds(String idsStr) {
        String[] split = idsStr.split(",");
        for (int i = 0; i < split.length; i++) {
            Oil oil = new Oil();
            String s = split[i];
            int i1 = Integer.parseInt(s);
            oil.setId(i1);
            oil.setIsdel(1);
            oilService.update(oil);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    public String time() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Oil selectOne(Integer id) {
        return this.oilService.queryById(id);
    }

}
